/**
  ******************************************************************************
  * @file    template.c
  * @author  Iron
  * @date    2022-01-01
  * @version v1.0
  * @brief   template c file
  */

/** @addtogroup GROUP_TEMPLATE
  * @{
  */

/* Private includes ----------------------------------------------------------*/
#include <stdio.h>
#include "contiki.h"
#include "contiki-net.h"
#include "sys/node-id.h"
#include "sys/autostart.h"
#include "sys/process.h"
#include "sys/procinit.h"
#include "sys/etimer.h"
#include "sys/rtimer.h"
#include "sys/ctimer.h"
#include "sys/clock.h"
#include "sys/energest.h"
#include "dev/watchdog.h"

/* Private define ------------------------------------------------------------*/
#define DEBUG 1

#if DEBUG
    #include <stdio.h>
    #define PRINTF(...) printf(__VA_ARGS__)
#else
    #define PRINTF(...)
#endif

/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
unsigned int idle_count = 0;
unsigned short node_id = 0;

/* Private function prototypes -----------------------------------------------*/
void platform_init_stage_one(void);
void platform_init_stage_two(void);
void platform_init_stage_three(void);
void platform_idle();

/*---------------------------------------------------------------------------*/
void
node_id_restore(void)
{
    uint8_t uid[8];

    GetUniqueID(uid);

    node_id = uid[0] | uid[1] << 8;
}

/*---------------------------------------------------------------------------*/
void
node_id_burn(unsigned short id)
{

}

/*---------------------------------------------------------------------------*/
int main()
{
    platform_init_stage_one();

    clock_init();
    rtimer_init();
    process_init();
    process_start(&etimer_process, NULL);
    ctimer_init();
    watchdog_init();
    energest_init();
#if STACK_CHECK_ENABLED
    stack_check_init();
#endif

    platform_init_stage_two();

#if QUEUEBUF_ENABLED
    queuebuf_init();
#endif /* QUEUEBUF_ENABLED */

    // node-id
    node_id_restore();

    platform_init_stage_three();

    autostart_start(autostart_processes);

    watchdog_start();

#if !(PLATFORM_PROVIDES_MAIN_LOOP)
    while (1)
    {
        uint8_t r;
        do
        {
            r = process_run();
            watchdog_periodic();
        }
        while (r > 0);

        platform_idle();
    }
#else
    platform_main_loop();
    return 0;
#endif
}


/**
  * @}
  */

/******************* (C)COPYRIGHT 2022 ***** END OF FILE *********************/
